package com.wing.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.user.model.entity.WareHouseCustomerVo;
import com.wing.user.model.entity.WarehouseCustomer;
import com.wing.user.service.WarehouseCustomerService;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
* (warehouse_customer)表控制层
*
* @author xxxxx
*/
@Api(value = "客户信息", tags = "#/warehouse/customer")
@RestController("WarehouseCustomerController")
@RequestMapping("/warehouse/admin/customer")
@RequiredArgsConstructor
public class WarehouseCustomerController {
/**
* 服务对象
*/
    @Autowired
    private WarehouseCustomerService warehouseCustomerService;
    
    @ApiOperation(value = "查询用户", notes = "搜索")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String", paramType = "form")
    })
    @GetMapping("/list")
    public PageResult<WareHouseCustomerVo> list(
            @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit
            ,@RequestParam(defaultValue = "create_time") String sort
            ,@RequestParam(defaultValue = "desc") String order
    ){

        Page<WareHouseCustomerVo> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        if(StrUtil.isNotBlank(sort)){
            paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        }
        if(StrUtil.isNotBlank(order)){
            paramMap.put("order",StrUtil.toUnderlineCase(order));
        }

        Page<WareHouseCustomerVo> list = warehouseCustomerService.pageList4app(pg, paramMap);

        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "添加企业基本信息", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(WarehouseCustomer.Add.class) WarehouseCustomer warehouseCustomer) {
        if (warehouseCustomerService.add(warehouseCustomer) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改企业基本信息", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated (WarehouseCustomer.Update.class) WarehouseCustomer warehouseCustomer) {
      return warehouseCustomerService.updateWarehouseCustomer(warehouseCustomer);
    }

    @ApiOperation(value = "删除企业基本信息", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "warehouseCustomerId", value = "企业基本信息id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}" )
    public JsonResult delete(@PathVariable("id") Long warehouseCustomerId) {
        return warehouseCustomerService.deleteWarehouseCustomer(warehouseCustomerId);
    }
}
